package com.mingweicai.intergration.springaop.demo1;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class LogAspect {

  @Pointcut("execution(public int com.mingweicai.intergration.springaop.demo2.BusinessService.*(..))")
  public void pointCut(){}

  /**
   * 前置同通知
   */
  @Before("pointCut()")
  public void logStart(){
    System.out.println("------@Before: 前置通知日志------");
  }

  /**
   * 后置通知
   */
  @After("pointCut()")
  public void logEnd(){
    System.out.println("------@After: 后置通知日志------");
  }

  /**
   * 只有方法成功返回才会执行
   * @param result
   */
  @AfterReturning(value = "pointCut()", returning = "result")
  public void logReturn(Object result){
    System.out.println("------@AfterReturning: 方法执行完毕,结果是 "+ result +"------");
    System.out.println();
  }

  @AfterThrowing(value = "pointCut()",throwing = "t")
  public void logException(Throwable t){
    System.out.println("------@AfterThrowing: 运行异常......异常信息是" + t);
  }

  @Around("pointCut()")
  public Object Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    System.out.println("------@Around: 执行目标方法之前...------");
    Object obj = proceedingJoinPoint.proceed();//相当于开始调div地
    System.out.println("------@Around: 执行目标方法之后...------");
    return obj;
  }
}
